﻿<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		
		<title>Tech11 - Login</title>

		<link type="text/css" href="/store/css/login.css" rel="stylesheet" />	
	</head>
	<body>
	<div id="container">
		<div class="logo">
			<a href="#"><img src="/store/img/logo.png" alt="" /></a>
		</div>
		<div id="box">
			<p class="main">
				<label>微信扫一扫登陆 </label>
				<img src="/wx/auth/login/qr?token={$token}" />
			</p>
		</div>
	</div>

	</body>
</html>

<script src="http://cdn.sockjs.org/sockjs-0.3.4.min.js"></script>
<script type="text/javascript">
var sock = new SockJS('/ws/socketio/');
sock.onopen = function(){
	console.log('open');

	send({
		action: 'wx-auth-login-mapping-token', token: '{$token}'});
};
sock.onclose = function(){
	console.log('close');
};

sock.onmessage = function(e){
	console.log(e.data);

	if(!e.data)
		return;

	var obj = JSON.parse(e.data);
	if(obj && 'reject' == obj.action){
		alert('用户拒绝登陆。。。');
		return;
	}

	if(obj && 'loginok' == obj.action){
		alert('login ok');
		document.location.href = '/wx/auth/login/ok?token={$token}';
		return;
	}
};

function send(message){
	if(sock.readyState === SockJS.OPEN){
		console.log('sending message')
		sock.send(JSON.stringify(message));
	}else{
		console.log('The socket is not open.');
	}
}

window.onbeforeunload = function(){
	sock.close();
};

</script>